﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;
using HousingInfo.Library;

namespace NEWS.Admin
{
    public partial class LienHe : System.Web.UI.Page
    {
        private static string mode="";
        private static ContactBO contactbo;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                mode = Request.QueryString["Mode"];                
                if (mode == "lienhe")
                {
                    lblcontact.Text = "Danh Sách Liên Hệ";
                    GridView1.Columns[0].Visible = false;
                    GridView1.Columns[4].Visible = false;
                    GridView1.Columns[5].Visible = false;
                    GridView1.Columns[6].Visible = false;
                    GridView1.Columns[7].Visible = true;
                    GridView1.Columns[8].Visible = true;
                    btnxoa.Visible = true;
                    GridView1.DataSource = ContactBOList.GetContactBOList_Value(false, false, true);
                    GridView1.DataBind();                    
                }
                else if(mode=="hotro")
                {
                    lblcontact.Text = "Danh Sách Hổ Trợ";
                    btnthem.Visible = true;
                    btnxoa.Visible = true;
                    dlhotro.Visible = true;
                    GridView1.Columns[1].Visible = false;
                    GridView1.Columns[4].Visible = true;
                    GridView1.Columns[5].Visible = true;
                    GridView1.Columns[6].Visible = true;
                    GridView1.Columns[7].Visible = false;
                    GridView1.Columns[8].Visible = false;
                    GridView1.DataSource = ContactBOList.GetContactBOList_Value(true, true, false);
                    GridView1.DataBind();
                }
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblcontactid.Visible = true;
            txtcontactid.Visible = true;
            message.Text = "";
            messagedelete.Text = "";
            int contactid = Convert.ToInt32(GridView1.SelectedRow.Cells[3].Text);
            contactbo = ContactBO.GetContactBO(contactid);
            txtcontactid.Text = contactbo.ContactID.ToString();
            txtname.Text = contactbo.Name;            
            txtcreatdate.Text = contactbo.CreadedDate.ToString();         
            isActive.Checked = contactbo.IsActive;
            if (dlhotro.SelectedIndex == 0)
            {
                lblyahoo.Visible = true;
                txtnickyahoo.Visible = true;
                Lblskyper.Visible = true;
                txtnickskyer.Visible = true;
                txtnickyahoo.Text = contactbo.IsYahoo;
                txtnickskyer.Text = contactbo.IsSkyper;
            }
            else if (dlhotro.SelectedIndex == 1)
            {
                lblyahoo.Visible = true;
                txtnickyahoo.Visible = true;
                Lblskyper.Visible = false;
                txtnickskyer.Visible = false;
                txtnickyahoo.Text = contactbo.IsYahoo;
            }
            else if (dlhotro.SelectedIndex == 2)
            {
                lblyahoo.Visible = false;
                txtnickyahoo.Visible = false;
                Lblskyper.Visible = true;
                txtnickskyer.Visible = true;
                txtnickskyer.Text = contactbo.IsSkyper;
            }
            txtcreatdate.Visible = true;
            lblcreatedate.Visible = true;
            btnAdd.Visible = false;
            btnUpdate.Visible = true;
            PanelActions.Visible = true;
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    string strScript = "SelectDeSelectHeader(" + ((CheckBox)e.Row.Cells[0].FindControl("chkSelect")).ClientID + ");";
                    ((CheckBox)e.Row.Cells[0].FindControl("chkSelect")).Attributes.Add("onclick", strScript);
                }
            }
            catch
            {
                //report error
            }        
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            int contactid = Convert.ToInt32(txtcontactid.Text);
            ContactBO contactbo1 = ContactBO.GetContactBO(contactid);
            contactbo1.Name = txtname.Text;
            contactbo1.IsActive = isActive.Checked;
            try
            {                
                if (dlhotro.SelectedIndex == 0)
                {
                    contactbo1.IsYahoo = txtnickyahoo.Text;
                    contactbo1.IsSkyper = txtnickskyer.Text;
                    contactbo1.Save();
                    GridView1.DataSource = ContactBOList.GetContactBOList_Value(true, true, false);
                    GridView1.DataBind();
                }
                else if (dlhotro.SelectedIndex == 1)
                {
                    contactbo1.IsYahoo = txtnickyahoo.Text;
                    contactbo1.Save();
                    GridView1.DataSource = ContactBOList.GetContactBOList_Value(true, false, false);
                    GridView1.DataBind();
                }
                else if (dlhotro.SelectedIndex == 2)
                {
                    contactbo1.IsSkyper = txtnickskyer.Text;
                    contactbo1.Save();
                    GridView1.DataSource = ContactBOList.GetContactBOList_Value(false, true, false);
                    GridView1.DataBind();
                }
                messagedelete.Text = "Cập nhật thành công !";
                PanelActions.Visible = false;
            }
            catch
            {
                message.Text = "Cập nhật thất bại!";
                messagedelete.Text = "";
            }
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
             contactbo = ContactBO.NewContactBO();
             contactbo.Name = txtname.Text;
             contactbo.CreadedDate = DateTime.Now;
             contactbo.IsActive = isActive.Checked;
             try
             {
                 if (dlhotro.SelectedIndex == 0)
                 {
                     contactbo.IsYahoo = txtnickyahoo.Text;
                     contactbo.IsSkyper = txtnickskyer.Text;
                     contactbo.Save();
                     GridView1.DataSource = ContactBOList.GetContactBOList_Value(true, true, false);
                     GridView1.DataBind();
                     messagedelete.Text = "Thêm thành công !";
                     PanelActions.Visible = false;

                 }
                 else if (dlhotro.SelectedIndex == 1)
                 {
                     contactbo.IsYahoo = txtnickyahoo.Text;
                     contactbo.Save();
                     GridView1.DataSource = ContactBOList.GetContactBOList_Value(true, false, false);
                     GridView1.DataBind();
                     messagedelete.Text = "Thêm thành công !";
                     PanelActions.Visible = false;
                 }
                 else if (dlhotro.SelectedIndex == 2)
                 {
                     contactbo.IsSkyper = txtnickskyer.Text;
                     contactbo.Save();
                     GridView1.DataSource = ContactBOList.GetContactBOList_Value(false, true, false);
                     GridView1.DataBind();
                     messagedelete.Text = "Thêm thành công !";
                     PanelActions.Visible = false;
                 }
             }
             catch
             {
                 message.Text = "Thêm thất bại !";
             }
       
        }

        protected void btndong_Click(object sender, EventArgs e)
        {
            PanelActions.Visible = false;
        }

        protected void dlhotro_SelectedIndexChanged(object sender, EventArgs e)
        {
          thaydoigrid();
          PanelActions.Visible = false;
          messagedelete.Text = "";
        }
        private void thaydoigrid()
        {
            if (dlhotro.SelectedIndex == 0)
            {
                lblcontact.Text = "Danh Sách Hổ Trợ";
                GridView1.Columns[5].Visible = true;
                GridView1.Columns[6].Visible = true;
                GridView1.DataSource = ContactBOList.GetContactBOList_Value(true, true, false);
                GridView1.DataBind();
            }
            else if (dlhotro.SelectedIndex == 1)
            {
                lblcontact.Text = "Danh Sách Hổ Trợ Yahoo";
                GridView1.DataSource = ContactBOList.GetContactBOList_Value(true, false, false);
                GridView1.Columns[6].Visible = false;
                GridView1.Columns[5].Visible = true;
                GridView1.DataBind();
            }
            else if (dlhotro.SelectedIndex == 2)
            {
                lblcontact.Text = "Danh Sách Hổ Trợ Skyper";
                GridView1.DataSource = ContactBOList.GetContactBOList_Value(false, true, false);
                GridView1.Columns[5].Visible = false;
                GridView1.Columns[6].Visible = true;
                GridView1.DataBind();
            }
        }

        protected void btnsend_Click(object sender, EventArgs e)
        {
            //thuc hien gui thong tin qua mail

        }

        protected void btndonglienhe_Click(object sender, EventArgs e)
        {
            pnlienhe.Visible = false;
        }

        protected void btnthem_Click(object sender, EventArgs e)
        {
            PanelActions.Visible = true;
            lblcontactid.Visible = false;
            txtcontactid.Visible = false;
            txtname.Text = "";
            message.Text = "";
            messagedelete.Text = "";
            txtnickskyer.Text = "";
            txtnickyahoo.Text = "";
            btnAdd.Visible = true;
            btnUpdate.Visible = false;
            lblcreatedate.Visible = false;
            txtcreatdate.Visible = false;
            if (dlhotro.SelectedIndex == 0)
            {
                lbHeaderActions.Text = "Thêm mới danh sách hỗ trợ trực tuyến";
                lblyahoo.Visible = true;
                txtnickyahoo.Visible = true;
                Lblskyper.Visible = true;
                txtnickskyer.Visible = true;
            }
            else if (dlhotro.SelectedIndex == 1)
            {
                lbHeaderActions.Text = "Thêm mới danh sách hỗ trợ Yahoo";
                lblyahoo.Visible = true;
                txtnickyahoo.Visible = true;
                Lblskyper.Visible = false;
                txtnickskyer.Visible = false;
            }
            else if (dlhotro.SelectedIndex == 2)
            {
                lbHeaderActions.Text = "Thêm mới danh sách hỗ trợ Skyper";
                lblyahoo.Visible = false;
                txtnickyahoo.Visible = false;
                Lblskyper.Visible = true;
                txtnickskyer.Visible = true;
            }
        }

        protected void btnxoa_Click(object sender, EventArgs e)
        {
            bool test = false;
            foreach (GridViewRow gvRow in GridView1.Rows)
            {
                CheckBox chkSel = (CheckBox)gvRow.FindControl("chkSelect");
                if (chkSel.Checked == true)
                {
                    try
                    {
                        test = true;
                        int contactid = Convert.ToInt32(gvRow.Cells[3].Text);
                        ContactBO.DeleteContactBO(contactid);
                    }
                    catch
                    {
                        messagedelete.Text = "Xóa thất bại";
                        return;
                    }
                }
            }
            if (test == false)
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Chưa chọn thông tin cần xóa !!!')", true);
                return;
            }
            else
            {
                messagedelete.Text = "Xóa thành công !";
                if (dlhotro.SelectedIndex == 0)
                {
                    GridView1.DataSource = ContactBOList.GetContactBOList_Value(true, true, false);
                    GridView1.DataBind();

                }
                else if (dlhotro.SelectedIndex == 1)
                {
                    GridView1.DataSource = ContactBOList.GetContactBOList_Value(true, false, false);
                    GridView1.DataBind();                   
                }
                else if (dlhotro.SelectedIndex == 2)
                {
                    GridView1.DataSource = ContactBOList.GetContactBOList_Value(false, true, false);
                    GridView1.DataBind();
                }
                PanelActions.Visible = false;
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Xem")
            {
                PanelActions.Visible = false;
                pnlienhe.Visible = true;
                GridViewRow selecrow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
                contactbo = ContactBO.GetContactBO(Convert.ToInt32(selecrow.Cells[3].Text));
                txtemail.Text = contactbo.Email;
                txttitle.Text = contactbo.Title;
                txtcontactcontent.Text = contactbo.ContactContent;
                txtngaygui.Text = contactbo.CreadedDate.ToString();
                txttraloi.Text = "";
                btnreply.Text = "Trả lời Email";
                btnreply.Visible = true;
                btnsend.Visible = false;
                Panel1.Visible = false;
            }
        }

        protected void btnreply_Click(object sender, EventArgs e)
        {
            if (btnreply.Text == "Trả lời Email")
            {
                btnsend.Visible = true;
                btnreply.Text = "Hủy";
                Panel1.Visible = true;
                txttitlefrom.Text = txttitle.Text;
            }
            else if (btnreply.Text == "Hủy")
            {
                txtfrom.Text = "";
                txttraloi.Text = "";
                btnreply.Text = "Trả lời Email";
                btnsend.Visible = false;
                Panel1.Visible = false;
            }
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            this.BindGrid();
        }

        public void BindGrid()
        {
            thaydoigrid();
        }
    }
}
